package com.ucn.groupawesome.archgame;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class BrickManager
{
	ArrayList<Brick> bricks;
	static BrickManager instance = null;

	public BrickManager()
	{
		bricks = new ArrayList<Brick>();
		importBricks("RED");
		importBricks("GREEN");
		importBricks("YELLOW");
	}

	public ArrayList<Brick> getAllBricks()
	{
		return bricks;
	}

	// TODO: better complexity :-(

	public ArrayList<String> getAllBricksExcept(ArrayList<Brick> except)
	{
		ArrayList<String> temp = getAllBricksNames();

		for (Brick b : bricks)
		{
			for (Brick c : except)
			{
				if (b.getName().equals(c.getName()))
				{
					temp.remove(b.getName());
				}
			}
		}

		return temp;
	}

	public static BrickManager getInstance()
	{
		if (instance == null)
		{
			instance = new BrickManager();
		}

		return instance;
	}

	public ArrayList<String> getAllBricksNames()
	{
		ArrayList<String> temp = new ArrayList<String>();

		for (Brick b : bricks)
		{
			temp.add(b.getName());
		}

		return temp;
	}

	public Brick findBrick(String name)
	{
		Brick br = null;

		for (Brick b : bricks)
		{
			if (b.getName().equals(name))
			{
				br = b;
			}
		}

		return br;
	}

	public void importBricks(String color)
	{
		InputStream input;

		try
		{
			input = getClass().getResourceAsStream("/assets/" + color + ".txt");
			BufferedReader in = new BufferedReader(new InputStreamReader(input, "ISO-8859-1"));
			String inputLine;

			while ((inputLine = in.readLine()) != null)
			{
				if (color.equals("RED"))
				{
					bricks.add(new Brick(inputLine, BrickCategory.RED));
				}
				else if (color.equals("YELLOW"))
				{
					bricks.add(new Brick(inputLine, BrickCategory.YELLOW));
				}
				else if (color.equals("GREEN"))
				{
					bricks.add(new Brick(inputLine, BrickCategory.GREEN));
				}

			}

			in.close();

		}
		catch (IOException e)
		{
			e.printStackTrace();

		}

	}
}
